﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace ExcelReport
{
    public delegate void ClockHander(string TimeString);
    public sealed class clock
    {
        Timer tmr = new Timer(1000);
        List<string> _clocks = new List<string>();
        public event ClockHander ClockEvent;

        public void AddClock(string TimeString)
        {
            _clocks.Add(TimeString);
        }
        public void RemoveClock(string TimeString)
        {
            _clocks.Remove(TimeString);
        }
        private clock()
        {
            tmr.Elapsed += tmr_Elapsed;
            tmr.Start();
        }

        public static readonly clock instance = new clock();


        void tmr_Elapsed(object sender, ElapsedEventArgs e)
        {
            foreach (var timeString in _clocks)
            {
                if (timeString == DateTime.Now.ToString("HH:mm:ss") && ClockEvent != null)
                {
                    ClockEvent(timeString);
                }
            }
        }
        public void AddSubscriber(ClockHander subscriber)
        {
            ClockEvent += subscriber;
        }
    }
}
